/*  
 *  Copyright 2006 Paul Jack.
 *
 *  This file is part of the Dex compiler suite. 
 *  
 *  Dex is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; either version 2 of the License, or (at your
 *  option) any later version.
 *  
 *  Dex is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 *  Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */
package dex.compiler.checker;


import dex.compiler.checker.type.BooleanType;
import dex.compiler.checker.type.ErrorType;
import dex.compiler.checker.type.IntegerType;
import dex.compiler.checker.type.Type;
import dex.compiler.model.base.Place;
import dex.compiler.model.expression.Unary;
import dex.compiler.problem.Reporter;
import static dex.compiler.checker.ExpressionProblems.*;


/**
 * The type checking rules for unary operators.
 */
class UnaryRules {


	/** Reporter used to report errors. */
	private Reporter reporter;
	
	
	/** The place to include in error reports. */
	private Place place;

	
	/** The operator to check. */
	private Unary unary;
	
	
	/** The operand to check. */
	private Type operand;
	

	/**
	 * Constructs a new UnaryRules.
	 * 
	 * @param reporter   the reporter to send errors to
	 * @param place      the place to include in error reports
	 * @param unary      the unary operator being checked
	 * @param operand    the operand type for that operator
	 */
	public UnaryRules(Reporter reporter, Place place, Unary unary, Type operand) {
		super();
		this.reporter = reporter;
		this.place = place;
		this.unary = unary;
		this.operand = operand;
	}


	/**
	 * Checks the unary operator expression.
	 * 
	 * @return  the type the expression evaluates to
	 */
	public Type check() {
		switch (unary) {
			case NEGATE:
				return checkInteger();
			case INVERT:
				return checkInteger();
			case NOT:
				return checkNot();
			default:
				throw new AssertionError();
		}
	}


	/**
	 * Checks an integer unary expression.
	 * 
	 * @return  the integer type of the operand, or the error type if
	 *   the operand wasn't an integer
	 */
	private Type checkInteger() {
		// FIXME: Support floating-point for - operator
		if (!(operand instanceof IntegerType)) {
			reporter.error(BAD_INTEGER_UNARY, place, operand.toString());
		}
		return operand;
	}


	/**
	 * Checks the not expression.
	 * 
	 * @return  the boolean type, or the error type if the operand type wasn't boolean
	 */
	private Type checkNot() {
		if (!(operand instanceof BooleanType)) {
			reporter.error(BAD_BANG, place, operand.toString());
			return ErrorType.INSTANCE;
		}
		return BooleanType.INSTANCE;
	}


}
